package logic

import (
	"Serve/global/model"
	"context"

	"Serve/VideoSrv"
	"Serve/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoDataLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoDataLogic {
	return &VideoDataLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoDataLogic) VideoData(in *VideoSrv.VideoDataRequest) (*VideoSrv.VideoDataResponse, error) {
	// todo: add your logic here and delete this line
	var VideoData struct {
		model.Video
		UserName  string
		ClassName string
	}
	if err := model.DB.Model(model.Video{}).Joins("join users on videos.user_id = users.id join classes on videos.class_id = classes.id").Where("videos.id = ?", in.VideoId).Select("videos.*,users.user_name,classes.class_name").Scan(&VideoData).Error; err != nil {
		return nil, err
	}
	return &VideoSrv.VideoDataResponse{
		Video: &VideoSrv.FormVideo{
			Title:     VideoData.Title,
			Class:     int64(VideoData.ClassId),
			Des:       VideoData.Des,
			Tag:       VideoData.Tag,
			VideoUrl:  VideoData.VideoUrl,
			ImageUrl:  VideoData.ImageUrl,
			UserId:    int64(VideoData.UserId),
			UserName:  VideoData.UserName,
			ClassName: VideoData.ClassName,
			TagName:   VideoData.Tag,
			ShowCount: int64(VideoData.ShowCount),
		},
	}, nil
}
